package com.boomsecret.comparator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author wangjinliang on 2018/12/6.
 */
public class ComparatorDemo {
    public static void main(String[] args) {
        List<User> userList = new ArrayList<>();
        userList.add(new User("a", 12));
        userList.add(new User("b", 12));
        userList.add(new User("c", 20));
        userList.add(new User("d", 2));

        // asc
        Collections.sort(userList, Comparator.comparingInt(User::getAge));
        System.out.println(userList.toString());

        // desc
        Collections.sort(userList, (user1, user2) -> user2.getAge() - user1.getAge());
        System.out.println(userList.toString());
    }
}
